package com.zenithmind.library.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zenithmind.library.pojo.domain.BookInventory;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 图书库存Mapper接口
 * 遵循接口隔离原则：只包含库存相关的数据访问方法
 * 
 * @author ZenithMind
 * @since 2024-06-14
 */
@Mapper
public interface BookInventoryMapper extends BaseMapper<BookInventory> {

    /**
     * 根据图书ID查询库存信息
     * 
     * @param bookId 图书ID
     * @return 库存信息
     */
    @Select("SELECT * FROM lib_book_inventory WHERE book_id = #{bookId} AND deleted = 0")
    BookInventory selectByBookId(@Param("bookId") String bookId);

    /**
     * 更新可借阅数量
     * 
     * @param bookId 图书ID
     * @param availableCopies 可借阅数量
     * @return 更新行数
     */
    @Update("UPDATE lib_book_inventory SET available_copies = #{availableCopies}, update_time = NOW() " +
            "WHERE book_id = #{bookId} AND deleted = 0")
    int updateAvailableCopies(@Param("bookId") String bookId, @Param("availableCopies") Integer availableCopies);

    /**
     * 更新已借出数量
     * 
     * @param bookId 图书ID
     * @param borrowedCopies 已借出数量
     * @return 更新行数
     */
    @Update("UPDATE lib_book_inventory SET borrowed_copies = #{borrowedCopies}, update_time = NOW() " +
            "WHERE book_id = #{bookId} AND deleted = 0")
    int updateBorrowedCopies(@Param("bookId") String bookId, @Param("borrowedCopies") Integer borrowedCopies);

    /**
     * 更新预约数量
     * 
     * @param bookId 图书ID
     * @param reservedCopies 预约数量
     * @return 更新行数
     */
    @Update("UPDATE lib_book_inventory SET reserved_copies = #{reservedCopies}, update_time = NOW() " +
            "WHERE book_id = #{bookId} AND deleted = 0")
    int updateReservedCopies(@Param("bookId") String bookId, @Param("reservedCopies") Integer reservedCopies);

    /**
     * 借出图书（原子操作）
     * 
     * @param bookId 图书ID
     * @return 更新行数
     */
    @Update("UPDATE lib_book_inventory SET " +
            "available_copies = available_copies - 1, " +
            "borrowed_copies = borrowed_copies + 1, " +
            "update_time = NOW() " +
            "WHERE book_id = #{bookId} AND available_copies > 0 AND deleted = 0")
    int borrowBook(@Param("bookId") String bookId);

    /**
     * 归还图书（原子操作）
     * 
     * @param bookId 图书ID
     * @return 更新行数
     */
    @Update("UPDATE lib_book_inventory SET " +
            "available_copies = available_copies + 1, " +
            "borrowed_copies = borrowed_copies - 1, " +
            "update_time = NOW() " +
            "WHERE book_id = #{bookId} AND borrowed_copies > 0 AND deleted = 0")
    int returnBook(@Param("bookId") String bookId);

    /**
     * 预约图书（原子操作）
     * 
     * @param bookId 图书ID
     * @return 更新行数
     */
    @Update("UPDATE lib_book_inventory SET " +
            "available_copies = available_copies - 1, " +
            "reserved_copies = reserved_copies + 1, " +
            "update_time = NOW() " +
            "WHERE book_id = #{bookId} AND available_copies > 0 AND deleted = 0")
    int reserveBook(@Param("bookId") String bookId);

    /**
     * 取消预约（原子操作）
     * 
     * @param bookId 图书ID
     * @return 更新行数
     */
    @Update("UPDATE lib_book_inventory SET " +
            "available_copies = available_copies + 1, " +
            "reserved_copies = reserved_copies - 1, " +
            "update_time = NOW() " +
            "WHERE book_id = #{bookId} AND reserved_copies > 0 AND deleted = 0")
    int cancelReservation(@Param("bookId") String bookId);

    /**
     * 检查库存是否充足
     * 
     * @param bookId 图书ID
     * @param requestedQuantity 请求数量
     * @return 是否充足
     */
    @Select("SELECT CASE WHEN available_copies >= #{requestedQuantity} THEN 1 ELSE 0 END " +
            "FROM lib_book_inventory WHERE book_id = #{bookId} AND deleted = 0")
    Boolean isStockSufficient(@Param("bookId") String bookId, @Param("requestedQuantity") Integer requestedQuantity);
}
